package com.ruoyi.algorithm.controller;

import com.ruoyi.algorithm.entity.Image;
import com.ruoyi.algorithm.entity.ImageVersion;
import com.ruoyi.algorithm.entity.TableStruct;
import com.ruoyi.algorithm.model.ListImageRequestModel;
import com.ruoyi.algorithm.service.ImageService;
import com.ruoyi.algorithm.service.TableStructService;
import com.beihu.sightplus.common.authorization.service.UserAuthorization;
import com.beihu.sightplus.common.response.ErrorMsgType;
import com.beihu.sightplus.common.response.PageAndConditionResult;
import com.beihu.sightplus.common.response.PageResult;
import com.beihu.sightplus.common.response.ResultData;
import io.swagger.annotations.*;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.util.List;

@RestController
@RequestMapping("/image")
@Api(value = "镜像管理", tags = {"镜像管理"})
public class ImageController {

    @Resource
    TableStructService tableStructService;
    @Resource
    private ImageService imageService;

    @PostMapping("/add")
    @ApiOperation("添加镜像接口")
    public ResultData add(@RequestBody Image image) {
        boolean result = imageService.addImage(image);
        if (result) {
            return ResultData.success("添加成功");
        } else {
            return ResultData.error(ErrorMsgType.Fail);
        }
    }

    @PutMapping("/uploadImage")
    @ApiOperation("上传镜像接口")
    public ResultData uploadImage(@RequestPart(value = "file")
                                  @ApiParam(value = "文件", type = "file")
                                          MultipartFile file,
                                  @ApiParam(value = "镜像id", type = "long")
                                          Long imageId) {
        ImageVersion imageVersion = imageService.UploadImage(file, imageId);
        return ResultData.success(imageVersion);
    }

    @PutMapping("/update")
    @ApiOperation("修改镜像接口")
    public ResultData update(@RequestBody
                             @ApiParam Image image) {
        boolean b = imageService.updateImage(image);
        if (b) {
            return ResultData.success(true);
        } else {
            return ResultData.error(ErrorMsgType.Fail);
        }
    }

    @PutMapping("/detail")
    @ApiOperation("镜像详情接口")
    public ResultData detail(Long id) {
        Image detail = imageService.detail(id);
        return ResultData.success(detail);
    }

    @DeleteMapping("/delete")
    @ApiOperation("删除镜像接口")
    public ResultData delete(Long id) {

        boolean b = imageService.deleteImage(id);
        if (b) {
            return ResultData.success(true);
        } else {
            return ResultData.error(ErrorMsgType.Fail);
        }
    }

    @GetMapping("/listImageVersion")
    @ApiOperation("查询镜像下的镜像版本")
    @ApiResponses({
            @ApiResponse(code = 200, message = "成功", response = ImageVersion.class)
    })
    public ResultData listImageVersion(Long id) {
        List<ImageVersion> imageVersions = imageService.listImageVersion(id);
        return ResultData.success(imageVersions);
    }

    @GetMapping("/listHistoryImageVersion")
    @ApiOperation("查询历史版本接口")
    public ResultData listHistoryImageVersion(@ApiParam("镜像id") Long imageId) {
        List<ImageVersion> historyImageVersion = imageService.listHistoryImageVersion(imageId);
        return ResultData.success(historyImageVersion);
    }

    @GetMapping("/recoverImageVersion")
    @ApiOperation("恢复历史版本接口")
    public ResultData recoverImageVersion(@ApiParam("镜像版本id") Long id) {
        boolean result = imageService.recoverImageVersion(id);
        if (result) {
            return ResultData.success(true);
        } else {
            return ResultData.error(ErrorMsgType.Fail);
        }
    }

    @DeleteMapping("/deleteImageVersion")
    @ApiOperation("删除历史版本接口")
    public ResultData deleteImageVersion(@ApiParam("镜像版本id") Long id) {
        boolean result = imageService.deleteImageVersion(id);
        if (result) {
            return ResultData.success(true);
        } else {
            return ResultData.error(ErrorMsgType.Fail);
        }
    }

    @GetMapping("/listImage")
    @ApiOperation("分页查询")
    public ResultData listImage(ListImageRequestModel model) {
        List<TableStruct> tableField = tableStructService.getTableField(UserAuthorization.getUserAuthorization().getId(), "image");
        PageResult pageResult = imageService.listImage(model);
        PageAndConditionResult result = new PageAndConditionResult(tableField, pageResult);

        return ResultData.success(result);
    }
}
